package com.lagou;

import com.lagou.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {

    private static final ExecutorService executorService = Executors.newFixedThreadPool(40);

    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        Executors
                .newSingleThreadScheduledExecutor()
                .scheduleWithFixedDelay(new TPReportExecutor(), 1, 5, TimeUnit.SECONDS);

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String hello = null;
                    try {
                        hello = service.methodA();
//                        System.out.println("result :" + hello);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String hello = null;
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                        hello = service.methodB();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    System.out.println("result :" + hello);
                }

            }
        });

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String hello = null;
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                        hello = service.methodC();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    System.out.println("result :" + hello);
                }

            }
        });
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
